package com.example.bigevent.service.impl;

import com.example.bigevent.mapper.UserMapper;
import com.example.bigevent.mapper.system.RoleMapper;
import com.example.bigevent.mapper.system.UserRoleMapper;
import com.example.bigevent.pojo.PageBean;
import com.example.bigevent.pojo.User;
import com.example.bigevent.pojo.system.Role;
import com.example.bigevent.pojo.system.UserRole;
import com.example.bigevent.service.UserService;
import com.example.bigevent.utils.JwtUtil;
import com.example.bigevent.utils.Md5Util;
import com.example.bigevent.utils.ThreadLocalUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;

    /**
     * 根据用户名查找用户信息
     */
    @Override
    public User findByUserName(String userName) {
        return userMapper.findByUserName(userName);
    }

    /**
     * 根据id查找用户信息
     */
    @Override
    public User findById(Integer id) {
        return userMapper.findById(id);
    }

    /**
     * 获取后台所有用户信息
     */
    @Override
    public PageBean<User> sysUserInfo(Integer pageNum, Integer pageSize, User user) {
        //开启分页
        PageHelper.startPage(pageNum,pageSize);
        List<User> userList=userMapper.selectSysUserList(user);
        //向下转型
        Page<User> page= (Page<User>) userList;
        PageBean<User> pageBean=new PageBean<>(page.getTotal(),page.getResult());
        return pageBean;
    }

    /**
     * 获取用户的角色
     */
    @Override
    public List<Role> roles() {
        Map<String,Object> map=ThreadLocalUtil.get();
        Integer id= (Integer) map.get("id");
        List<Integer> roleIds = userRoleMapper.selectRoleIdsByUserId(id);
        List<Role> roleList = roleIds.stream()
                .map(roleId->roleMapper.findByRoleId(roleId)).collect(Collectors.toList());
        return roleList;
    }

    /**
     *注册用户
     */
    @Override
    @Transactional
    public void register(User user) {
        //对密码进行BCrypt加密
        String encode = passwordEncoder.encode(user.getPassword());
        user.setPassword(encode);

        userMapper.insert(user);
        //为用户分配权限
        Role role = roleMapper.findByRoleName("普通用户");
        UserRole userRole=new UserRole(user.getId(),role.getRoleId());
        userRoleMapper.insert(userRole);
    }

    /**
     * 更新用户
     */
    @Override
    public void update(User user) {
        //对密码进行BCrypt加密
        if(user.getPassword()!=null){
            String encode = passwordEncoder.encode(user.getPassword());
            user.setPassword(encode);
        }
        if(user.getId()==null){
            Map<String,Object> map=ThreadLocalUtil.get();
            Integer id= (Integer) map.get("id");
            user.setId(id);
        }
        //更新
        userMapper.update(user);
    }

    /**
     * 删除后台用户
     */
    @Override
    public void sysDelete(Integer id) {
        userMapper.deleteById(id);
    }
}
